<template>
  <div class="login-container">
    <div class="login-box">
      <h2 class="login-title">用户登录</h2>
      <el-form
        ref="loginFormRef"
        :model="loginForm"
        :rules="rules"
        label-width="80px"
        class="login-form"
      >
        <el-form-item label="用户名" prop="username">
          <el-input v-model="loginForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input type="password" v-model="loginForm.password"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script lang="ts">
import { defineComponent, reactive, ref } from 'vue'
import type { FormInstance, FormRules } from 'element-plus'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import axios from 'axios'
import type { AxiosResponse, AxiosError } from 'axios'

export default defineComponent({
  name: 'LoginPage',
  setup() {
    const loginForm = reactive({
      username: '',
      password: ''
    })

    const rules = reactive<FormRules>({
      username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
      password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
    })

    const loginFormRef = ref<FormInstance>()
    const router = useRouter()

    const submitForm = async () => {
      if (!loginFormRef.value) return
      await loginFormRef.value.validate((valid) => {
        if (valid) {
          // 发送登录请求到服务端进行验证
          axios
            .post('/api/login', loginForm)
            .then((response: AxiosResponse) => {
              // 指定 response 的类型为 AxiosResponse
              const { code, message, data } = response.data
              if (code === 200) {
                console.log('登录成功!')
                // 登录成功后,将 token 存储在本地存储中
                localStorage.setItem('token', data.token)
                // 跳转到 Dashboard 页面
                router.push('/dashboard')
              } else {
                console.log(message)
                ElMessage.error(message)
              }
            })
            .catch((error: AxiosError) => {
              // 指定 error 的类型为 AxiosError
              console.log('请求错误:', error)
              ElMessage.error('请求错误,请稍后重试')
            })
        } else {
          console.log('表单验证失败!')
          return false
        }
      })
    }

    return {
      loginForm,
      rules,
      loginFormRef,
      submitForm
    }
  }
})
</script>

<style scoped>
body, html {
  margin: 0;
  padding: 0;
}

.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: linear-gradient(135deg, #e0c3fc 0%, #8ec5fc 100%);
}

.login-box {
  width: 400px;
  padding: 40px;
  background: #fff;
  border-radius: 10px;
  box-shadow: 0px 0px 20px rgba(0, 0, 0, 0.1);
}

.login-title {
  text-align: center;
  margin-bottom: 30px;
  color: #6a67ce;
  font-size: 24px;
}

.login-form {
  margin-top: 20px;
}

.login-form .el-form-item {
  margin-bottom: 20px;
}

.el-button--primary {
  width: 100%;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border: none;
}
</style>
